/*************************************************
 *FileName:      convert.js
 *Description:   兑换换币.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var omoneysub = 0; 
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
        //加载
        lode();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        $('.useragrre').on('tap',function(){
            $(this).toggleClass('active');
        })
        
        $('.useragrre-a').on('tap',function(event){
            common.pageTransition('useragrre.html?that=convert');
            return false;
        })
        
        $('.balance').on('input',function(){
            var obj = $('.balance').val();
            obj = obj.replace(/[^\d.]/g,"");
            obj = obj.replace(/^\./g,""); 
            obj = obj.replace(/\.{2,}/g,"."); 
            obj = obj.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            obj = obj.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
            $('.balance').val(obj);
            var oval =obj*omoneysub;
            $('.link-money em').text(oval);
        })
        
        
    }
    /**
     * 
     * 申请兑换
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    $('.rdntn').on('tap',function(){
        var oval =$('.balance').val();
        if(!$('.useragrre').hasClass('active')){
            mui.toast('请同意协议!');
            return false;
        }else{
            sexnum(oval);
        }
    })
    /**
     * lode
     * 加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function lode(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/exchangeInfo",
            dataType:'jsonp',
            data: {uid:uid,token:token},
            success: function(data) {
                if(data.error_code == 0){
                    $('.link-xun em').text(data.data.withdrawals_balance);
                    $('.notecase-text em').text(data.data.withdrawals_balance);
                    omoneysub=parseInt(data.data.rates);
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                }
            }  
        });
    }
    /**
     * sexnum
     * 兑换魅力勋章
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function sexnum(amount){
        common.loadingIn();
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/exchangeWithdrawalMoney",
            dataType:'jsonp',
            data: {uid:uid,token:token,amount:amount},
            success: function(data) {
                if(data.error_code == 0){
                    mui.toast('兑换成功！')
                    setTimeout(function(){mui.back();},1000)
                    var list = plus.webview.currentWebview().opener();//触发父页面的自定义事件(notecase2),从而进行刷新
                    mui.fire(list, 'notecase2');//返回true,继续页面关闭逻辑
                    return true;
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    mui.toast(data.error_msg);
                }
                common.loadingOut();
            }  
        });
    }